let s:current_file = expand('<sfile>')
let s:current_directory = fnamemodify(s:current_file, ':p:h')
let s:current_project = fnamemodify(s:current_directory, ':h')

function! s:on_exit(job, status)
    echomsg 'Job ' . a:job . 'exit with status ' a:status
endfunction

let s:cmdline = "git pull"
let s:opts = { 'out_io': 'buffer', 'out_msg': v:false }
let s:buf = "*git*"->bufadd()
let s:job = ''

function! s:on_out(channel, msg)
    let buf = ch_getbufnr(a:channel, "out")
    let job = ch_getjob(a:channel)
    let info = job_info(job)
    if type(info.cmd) == v:t_list
        let cmdline = join(info.cmd)
    else
        let cmdline = info.cmd
    endif
    let line = s:opts.cwd . ' $ ' . cmdline
    call appendbufline(buf, 0, line)
endfunction

function! bootstrap#update#config(...) abort
    if a:0 < 1
        let l:directory = s:current_project
    else
        let l:directory = a:1
    endif
    call deletebufline(s:buf, 1, "$")
    let s:opts = extend(s:opts, {
                \   'cwd': l:directory,
                \   'out_buf': s:buf,
                \   'out_cb': function('s:on_out')
                \ }, "force")
    let s:job = job_start(s:cmdline, s:opts)
endfunction

